var Friends = Class.create();
Object.extend(Friends, Attention);
Object.extend(Friends, {
	add: function(uid){
		new Ajax.Request("/~friends.index.prepare",{
			method: 'post',
			parameters: 'add_uid='+uid,
			onComplete: this.prepare.bindAsEventListener(this)
		});
	},
	bind: function(uid){
		new Ajax.Request("/~friends.index.bind",{
			method: 'post',
			parameters: 'quest_uid='+uid,
			onComplete: function(){}
		});
	},
	refuse: function(uid){
		new Ajax.Request("/~friends.index.refuse",{
			method: 'post',
			parameters: 'quest_uid='+uid,
			onComplete: function(){}
		});
	},	
	ignore: function(uid){
	},	
	confirmRemoveFriend: function(uid){
		new MessageBox(MessageBox.TYPE_YES_NO, '继续操作你和该用户将不再是好友关系', '取消好友', {yes: this.removeFriend.bind(this, uid)});
	},
	confirmRemoveRequest: function(uid){
		new MessageBox(MessageBox.TYPE_YES_NO, '继续操作将取消对该用户的好友邀请', '取消好友邀请', {yes: this.removeFriend.bind(this, uid)});
	},
	removeFriend: function(uid){
		new Ajax.Request("/~friends.index.remove_invit",{
			method: 'post',
			parameters: 'quest_uid='+uid,
			onComplete: this.onRemoveFriendComplete.bindAsEventListener(this)
		});
	},
	prompt: function(){
		new MessageBox(MessageBox.TYPE_PROMPT, '输入对方的uid或用户名', '添加好友', {ok: this.addRequest});
	},
	addRequest: function(uid, messagebox){
		new Ajax.Request("/~friends.index.add",{
			method: 'post',
			parameters: 'add_uid='+uid,
			onComplete: function(transport){
				enable_form();
				var ret = (transport.responseText).evalJSON();
				if(ret.result){
					messagebox.close();
					window.location.reload();
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '提示', {close:function(){}});
				}
			}
		});
		disable_form();
	},
	formRequest:function(form){
		if(form.add_uid.value.trim()==''){
			return false;
		}
		$(form).request({
			onComplete: this.prepare.bindAsEventListener(this)
		});
		return false;
	},
	prepare: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			ret.data.width = 415;
			new MessageBox(MessageBox.TYPE_ADDFRIEND, '', '', {
				ok: this.msgAdd
			}, ret.data);
		}else{
			if (ret.reason == 'err_not_login') {
				ret.reason = '请先登录';
			}
			new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '提示', {close:function(){}});
		}
	},
	msgAdd: function(){
		if($('add_friend_form').message.value.trim()==''){
			$('no_reason').innerHTML = '请说明你是谁或加对方为好友的理由';
			return false;
		}
		$('no_reason').innerHTML = '正在提交请求，请稍候...';
		$('add_friend_form').request({
			asynchronous: false,
			onComplete: function(transport){
				var ret = (transport.responseText).evalJSON();
				if(ret.result){
					window.location.href= '/~friends.index.get_list/friend/0';
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '提示', {close:function(){}});
				}
			}
		});

		return true;
	},
	onAddComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			location.reload();
		}else{
			MessageBox.showError(ret);
		}
	},
	onRemoveFriendComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			location.reload();
		}else{
			enable_form();
			MessageBox.showError(ret);
		}
	},
	
	
	//删除好友
	delFriendFrom: function(uid, group){
		var html = '<div class="friend_pw_mainr3">';
		html	+= '<p>您确认要删除该好友?</p>';
		html	+= '<p>删除后该好友将不再出现在您的好友列表中，同时您也将不再出现在TA的好友列表中。</p>';
		html	+= '</div>';	
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '删除好友', {ok: this.delFriend.bind(this, uid, group)});
	},
	
	delFriend: function(uid, group){
		disable_form();
		new Ajax.Request("/~friends.index.remove",{
			method: 'post',
			parameters: $H({quest_uid: uid, group: group}).toQueryString(),
			onComplete: this.onRemoveFriendComplete.bindAsEventListener(this)
		});
	},
	
	//修改备注表单
	remarkFrom: function(u) {
		var remarkId 	  = 'remark_' + u;
		var remarkInputId = 'remark_input_' + u;
		var reamrkTextId  = 'remark_text_' + u;
		$(remarkId).hide();
		$(remarkInputId).show();
		$(reamrkTextId).focus();
	},
	
	//修改备注
	editRemark: function(t, b){
		var fid	   = t.name;
		var remark = t.value;
		var reamrkTextId  = 'remark_text_' + fid;
		if (remark.empty()) {
			new MessageBox(MessageBox.TYPE_MESSAGE, '备注名称不能为空', '提示', {close:function(){$(reamrkTextId).focus();}});
			return false;
		} 
		var reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]{1,15}$/;
		if (!reg.test(remark)) {
			new MessageBox(MessageBox.TYPE_MESSAGE, '备注只能由1-15位字母,数字,下划线,中文组成', '提示', {close:function(){$(reamrkTextId).focus();}});
			return false;
		}
		var remarkId 	  = 'remark_' + fid;
		var remarkInputId = 'remark_input_' + fid;
		new Ajax.Request("/~friends.index.remark",{
			method: 'post',
			parameters: 'fid=' + fid + '&remark=' + remark,
			onComplete: function(transport){
				var ret = (transport.responseText).evalJSON();
				if (ret.result) {
					t.value = ret.data.remark;
					var editFlag = ' <a href="javascript:void(0)" class="modity-classify" onclick="javascript:Friends.remarkFrom(' + fid + ')" title="点击修改备注"></a>';
					if (b) {
						$(remarkId).update('<span class="home_remark">备注：' + ret.data.remark + editFlag + '</span>');
					} else {
						$(remarkId).update(ret.data.remark + editFlag);
					}
					
					$(remarkId).show();
					$(remarkInputId).hide();
				} else {
					MessageBox.showError(ret);
				}
			}
		});
	},

	//增加分组框
	addGroupFrom: function(){
		new MessageBox(MessageBox.TYPE_CUSTOM, $('group_from').innerHTML, '添加分组', {ok: this.addGroup});
	},
	
	//增加好友分组
	addGroup: function() {
		var group_name = $F('group_name');
		if (group_name.trim() == '') {
			alert('分组名称不能为空');
			return false;
		}
		disable_form();
		new Ajax.Request('/~friends.group.add_group',{
			parameters: $H({group_name: group_name}),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						window.location.reload();
					}else{
						enable_form();
						alert(ret.descriptions);
					}
				} catch (e){}
			}
		});
	},
	
	//编辑好友框
	editGroupFrom: function(gid, gname) {
		var html = '<div style="text-align:center;">';
		html	+= '分组名称: <input type="text" id="group_name" name="group_name" value="' + gname + '" size="30">';
		html	+= '<input type="hidden" id="group_id" name="group_id" value="' + gid + '">';
		html	+= '</div>';
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '编辑分组', {ok: this.editGroup});
	},
	
	//编辑好友分组
	editGroup: function() {
		var group_name = $F('group_name');
		if (group_name.trim() == '') {
			alert('分组名称不能为空');
			return false;
		}
		var group_id = $F('group_id');
		
		disable_form();
		new Ajax.Request('/~friends.group.edit_group',{
			parameters: $H({group_name: group_name, group_id: group_id}).toQueryString(),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						window.location.href= '/~friends.group.get_list/group/' + group_id;
					}else{
						enable_form();
						alert(ret.descriptions);
					}
				} catch (e){}
			}
		});
	},
	
	//删除好友分组确认框
	delGroupFrom: function(gid) {
		var html = '<div class="friend_pw_mainr">';
		html	+= '<p>你确认要删除本好友分组？</p>';
		html	+= '<p>删除该好友分组后，该分组好友将转移至未分组好友。</p>';
		html	+= '</div>';	
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '删除分组', {ok: this.delGroup.bind(this, gid)});
	},
	
	//删除好友分组
	delGroup: function(gid) {
		disable_form();
		new Ajax.Request("/~friends.group.del_group",{
			method: 'post',
			parameters: 'group_id=' + gid,
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						window.location.href= '/~friends.group.get_list';
					}else{
						enable_form();
						messagebox.close();
						MessageBox.showError(ret);
					}
				} catch (e){}
			}
		});
	},
	
	//把好友从分组中移除提示框
	delFriendGroupFrom: function(gid, fid, fname) {
		var html = '<div class="friend_pw_mainr3">';
		html	+= '<p>您确定要将' + fname + '从当前分组中删除?</p>';
		html	+= '<p>提示：该好友不会从您的好友列表中删除。</p>';
		html	+= '</div>';	
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '从分组移除好友', {ok: this.delFriendGroup.bind(this, gid, fid)});
	},
	
	//从分组中移除
	delFriendGroup: function(gid, fid) {
		disable_form();
		new Ajax.Request("/~friends.group.del_friend_group",{
			method: 'post',
			parameters: $H({group_id: gid, friend_id: fid}).toQueryString(),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						window.location.href= '/~friends.group.get_list/group/' + gid;
					}else{
						enable_form();
						messagebox.close();
						MessageBox.showError(ret);
					}
				} catch (e){}
			}
		});
	},
	
	//添加好友到分组框
	addFriendToGroupFrom: function(fid){
		new Ajax.Request("/~friends.group.get_friend_group",{
			method: 'post',
			parameters: 'friend_id=' + fid,
			onComplete: function(transport) {
				var ret = (transport.responseText).evalJSON();
				if (ret.result) {				
					var groupdata = ret.data;
					var groupleng = groupdata.length;
					var html = '<div class="friend_pw_mainr3">';
					html	+= '请选择分组：';
					html	+= '<div class="friend_pw_group">';
	
					for (var i = 0; i < groupleng; i++) {
						var checked = '';
						if (groupdata[i].checked == 1) {
							checked = 'checked';
						}
						if (i % 4 == 0) {
							html += '<ul>';	
						}
						
						html += '<li><input type="checkbox" name="group"  ' + checked + ' value="' + groupdata[i].group_id + '" /> ' + groupdata[i].group_name + '</li>';
						
						if (((i + 1) % 4 == 0) || (i + 1 == groupleng)) {
							html += "</ul>";	
						}
					}
					
					html += '</div>';
					html += '</div>';
			
					//alert(html);
					new MessageBox(MessageBox.TYPE_CUSTOM, html, '好友分组', {ok: this.addFriendToGroup.bind(this, fid)});
				} else {
					MessageBox.showError(ret);
				}
			}.bindAsEventListener(this)
		});
	},				
	
	//添加好友到分组
	addFriendToGroup: function(fid) {
		var groups = '';
		 $$('input[type="checkbox"][name="group"]').select(function(i){return i.checked}).each(function(i) {
		 	groups += groups == '' ? '' : ',';
		    groups += i.value;
		 });
		
		 disable_form();
		 new Ajax.Request("/~friends.group.add_friend_group",{
			method: 'post',
			parameters: $H({friend_id: fid, group_id: groups}).toQueryString(),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					
					if(ret.result){
						window.location.href = window.location.href
					}else{
						messagebox.close();
						MessageBox.showError(ret);
						enable_form();
					}
				} catch (e){}
			}
		});
	},
	
	
	
	
	//再次邀请弹出框
	invite_again_from: function (avatar, id, msn) {	
		var html = '<div class="invate-again">';
			html+= '<div class="imgborder">';
			html+= '<img class="avatar" src="' + avatar + '"/>';
			html+= '</div>';
			html+= '<div class="invate-message">';
			html+= '<p class="add">地址：' + msn + '</p>';
			html+= '<p>给TA的附加信息：</p>';
			html+= '<p><textarea name="" ></textarea></p>';
			html+= '</div>';
			html+= '</div>';
			
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '再次邀请', {ok: this.invite_again.bind(this, id)});
	},
	
	invite_again: function(id) {
		new Ajax.Request("/~friends.invitation.invite_email",{
			method: 'post',
			parameters: 'mid=' + id,
			onComplete: function(transport){
				var ret = transport.responseText.evalJSON();
				if (ret.result) {
					window.location.reload();
				} else {
					alert('发送邀请失败,请刷新页面重试');
				}
			}
		});
	},
	
	//其他方式再邀请弹出框
	invite_again_other_from: function(msn_id) {
		new Ajax.Request("/~friends.invitation.invite_again",{
			method: 'post',
			parameters: 'msn_id=' + msn_id,
			onComplete: function(transport){
				var ret = transport.responseText.evalJSON();
				if (ret.result) {
					new MessageBox(MessageBox.TYPE_HTML, ret.data, '', {ok: function(){}});
					$$('select').each(function(item){
						$('changeRole').setStyle({visibility: 'visible'});
					});
					$$('select').each(function(item) { item.show();});
				} else {
					new MessageBox(MessageBox.TYPE_MESSAGE, '获取数据失败,请刷新页面重试', '提示', {close:function(){}});
				}
			}.bindAsEventListener(this)
		});
	},
	
	//删除邀请
	delete_msn_invite: function(id, msn) {
		new Ajax.Request("/~friends.invitation.delete_msn_invite",{
			method: 'post',
			parameters: 'id=' + id +'&msn=' + msn,
			onComplete: function(transport){
				window.location.reload();
			}
		});
	},
	
	//保存邀请留言
	save_desc: function (status) {
		var txt = $('desc').value;
		new Ajax.Request('/~friends.invitation.save_desc/friend/1', {
			parameters: $H({desc: txt, type: status}).toQueryString(),
			onComplete: function(transport) {
				if (status == '1') {
					location.href = '/~friends.invitation.invite_form/friend/1';
				} else {
					$('desc_msg').show();
					new PeriodicalExecuter(function(pe) {   
						new Deletion('desc_msg');
						pe.stop(); 
					}, 1.5);	
				}
			}
		})
	},
	
	//armory邀请留言
	save_armory_desc: function (status) {
		var txt = $('armory_desc').value;
		new Ajax.Request('/~friends.invitation.save_armory', {
			parameters: $H({armory_desc: txt, type: status}).toQueryString(),
			onComplete: function(transport){
				if (status == '1') {
					location.href = '/~friends.invitation.invite_form';
				} else {
					$('armory_desc_msg').show();
					new PeriodicalExecuter(function(pe) {   
						new Deletion('armory_desc_msg');
						pe.stop(); 
					}, 1.5);
				}				
			}
		})
	},
	
	//msn导入
	msn_guide: function() {
		var username = $('username').value;
		var reg=/^[A-Za-z0-9+]+[A-Za-z0-9\.\_\-+]*@([A-Za-z0-9\-]+\.)+[A-Za-z0-9]+$/;
		if (!reg.test(username)) {
			new MessageBox(MessageBox.TYPE_MESSAGE, '请输入正确的MSN帐号', '提示', {close:function(){}});
			$('username').focus();
			return false;
		}
		var password = $('password').value;
		
		if (password == '') {
			new MessageBox(MessageBox.TYPE_MESSAGE, '请输入密码', '提示', {close:function(){}})
			$('password').focus();
			return false;
		}
		
		$('msn').disabled = true;
		$('invite_loadimg').show();
		$('invite_from').hide();
		$('invite_result').hide();
		
		var scroll_top = document.documentElement.scrollTop;
		document.documentElement.scrollTop = 0;
	
		new Ajax.Request('/~friends.invitation.msn', {
			method: 'post',
			parameters: 'username=' + username + '&password=' + password + '&r=' + Math.random(),
			onComplete: function(transport){
				$('invite_loadimg').hide();
				$('msn').disabled = false;
				var ret = transport.responseText.evalJSON();
				if (ret.result) {
					$('invite_result').update(ret.data);
					$('invite_result').show();
					$('invite_from').hide();
				} else {
					$('invite_result').hide();
					$('invite_from').show();
					var error_msg = '';
					if (ret.reason == 1) {
						error_msg = '用户名密码不能为空';
					} else if (ret.reason == 2) {
						error_msg = '请输入正确的用户名和密码';
					} else if (ret.reason == 3) {
						error_msg = '获取失败请重试';
					}
					
					document.documentElement.scrollTop = scroll_top;
					new MessageBox(MessageBox.TYPE_MESSAGE, error_msg, '提示', {close:function(){}});
				}
			}.bindAsEventListener(this)
		})
	},
	

	addRecommend: function(uid, msg){
		new Ajax.Request("/~friends.index.prepare",{
			method: 'post',
			parameters: 'add_uid='+uid,
			onComplete: this.prepareRecommend.bindAsEventListener(this, msg)
		});
	},

	prepareRecommend: function(transport, msg){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			ret.data.width = 415;
			new MessageBox(MessageBox.TYPE_ADDFRIEND, '', '', {
				ok: this.AddRecommend
			}, ret.data);
			$('add_friends_msg').value = msg;
		}else{
			if (ret.reason == 'err_not_login') {
				ret.reason = '请先登录';
			}
			new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '提示', {close:function(){}});
		}
	},

	AddRecommend: function(){
		if($('add_friend_form').message.value.trim()==''){
			$('no_reason').innerHTML = '请说明你是谁或加对方为好友的理由';
			return false;
		}
		$('no_reason').innerHTML = '正在提交请求，请稍候...';
		$('add_friend_form').request({
			asynchronous: false,
			onComplete: function(transport){
				var ret = (transport.responseText).evalJSON();
				if(ret.result){
					window.location.reload();
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '提示', {close:function(){}});
				}
			}
		});

		return true;
	},
	loadRecommend: function(page) {
		new Ajax.Request("/~friends.armory.embed",{
			method: 'get',
			parameters: 'page=' + page + '&r=' + Math.random(),
			onComplete: function(transport){
				var ret = transport.responseText;
				if (ret) {
					$('recommend_div').update(ret);
				} else {}
			}
		});
	},	
	search:function(k){
//		$('search').observe('click',function(){
			var v = $(k).value.trim();
			if(!v || v.length<=0 ){
				new MessageBox(MessageBox.TYPE_MESSAGE, '请输入关键词搜索', '提示', {close:function(){
					$(k).focus();
					}});
				return false;
			}
			location.href = "/~friends.group.search/?k="+escape(v);
//		});
	}
	
});